package com.xsy.localgame.puzzle;

import android.graphics.Bitmap;

import java.util.ArrayList;
import java.util.List;

public class ImageSplitter {

    public static List<ImagePiece> split(Bitmap bitmap, int piece) {

        List<ImagePiece> pieceList = new ArrayList<>();
        int width = bitmap.getWidth();
        int heidht = bitmap.getHeight();

        int pieceWidth = Math.min(width,heidht) / piece;
        for (int i = 0; i < piece; i++) {
            for (int j = 0; j < piece; j++) {
                ImagePiece imagePiece = new ImagePiece();
                imagePiece.index = j + i * piece;
                int xValue = j * pieceWidth;
                int yValue = i * pieceWidth;

                imagePiece.bitmap = Bitmap.createBitmap(bitmap,xValue,yValue,
                        pieceWidth,pieceWidth);
                pieceList.add(imagePiece);
            }
        }
        return pieceList;
    }
}
